package com.crm.common.utils;

import java.util.Random;

/**
 * 随机字符串生成工具类
 */
public class RandomStringGenerator {
    
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final Random RANDOM = new Random();
    
    /**
     * 生成4-6位随机字母字符串
     * 
     * @return 随机字符串，长度为4-6位，仅包含字母
     */
    public static String generateRandomString() {
        // 随机生成4-6之间的长度
        int length = RANDOM.nextInt(3) + 4; // 3表示范围(6-4+1)，4是基础值
        StringBuilder sb = new StringBuilder(length);
        
        // 生成随机字符串
        for (int i = 0; i < length; i++) {
            int randomIndex = RANDOM.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        
        return sb.toString();
    }
    
    /**
     * 生成指定长度的随机字母字符串
     * 
     * @param length 字符串长度(必须在4-6之间)
     * @return 随机字符串
     * @throws IllegalArgumentException 当长度不在4-6范围内时抛出异常
     */
    public static String generateRandomString(int length) {
        if (length < 4 || length > 6) {
            throw new IllegalArgumentException("长度必须在4-6之间");
        }
        
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = RANDOM.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        
        return sb.toString();
    }
}
